package tgdzWin

import (
	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
	"tgdz"

	"jk/jklog"
	"strconv"
)

type tgdzWindow struct {
	*walk.MainWindow
	submit       *walk.PushButton
	inputYear    *walk.TextEdit
	outputResult *walk.TextEdit
}

func (t *tgdzWindow) giveResult() {
	year, _ := strconv.Atoi(t.inputYear.Text())
	if tgdz.CalTGDZ(year) == "" {
		t.outputResult.SetText("sb 不要乱写")
	} else {
		result := t.inputYear.Text() + " 是 " + tgdz.CalTGDZ(year) + " 年"
		t.outputResult.SetText(result)
	}
}

func TgdzOut(parent walk.Window) {
	mw := new(tgdzWindow)

	main := MainWindow{
		AssignTo: &mw.MainWindow,
		Title:    "天干地支年结果",
		MinSize:  Size{400, 320},
		Layout:   VBox{},
		Children: []Widget{
			TextEdit{
				AssignTo: &mw.inputYear,
				// ReadOnly: true,
			},
			PushButton{
				AssignTo:  &mw.submit,
				Text:      "查查看",
				OnClicked: mw.giveResult,
			},
			TextEdit{
				AssignTo: &mw.outputResult,
				ReadOnly: true,
			},
		},
	}

	err := main.Create()
	if err != nil {
		jklog.L().Fatal(err)
	}

	mw.Run()
}
